package com.yy.database;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * 数据库操作类
 * 
 * @author liyuye
 * @date 2015-5-18 上午10:32:05
 */
public class DBHelperService {
	public DBHelper helper;

	public DBHelperService(Context ctx) {
		if (ctx != null) {
			helper = new DBHelper(ctx);
		}
	}

	/**
	 * 初始化系统数据
	 * 
	 * @param db
	 * @param is
	 */
	public boolean initData(Reader reader) {
		boolean bool = false;
		if (reader != null) {
			BufferedReader br = null;
			SQLiteDatabase db = helper.getWritableDatabase();
			try {
				db.beginTransaction();
				br = new BufferedReader(reader);
				String str;
				while ((str = br.readLine()) != null) {
					if (str != null) {
						try {
							db.execSQL(str);
						} catch (Exception e) {
							continue;
						}

					}
				}
				db.setTransactionSuccessful();
				bool = true;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				db.endTransaction();
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return bool;
	}

	public void executeSql(String sql) {
		SQLiteDatabase db = helper.getWritableDatabase();
		if (sql != null) {
			db.beginTransaction();
			try {
				db.execSQL(sql);

			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			db.setTransactionSuccessful();
			db.endTransaction();
		}
		db.close();
	}

	/**
	 * 插入记录
	 * 
	 * @param DATABASE_TABLE
	 * @param sqlstr
	 * @return
	 */
	public long insertRecord(String DATABASE_TABLE, ContentValues sqlstr) {
		SQLiteDatabase db = helper.getWritableDatabase();
		long l = db.insert(DATABASE_TABLE, null, sqlstr);
		return l;
	}

	/**
	 * 删除一条记录
	 * 
	 * @param DATABASE_TABLE
	 * @param pkstr
	 * @return
	 */
	public boolean deleteRecord(String DATABASE_TABLE, String pkstr) {
		SQLiteDatabase db = helper.getWritableDatabase();
		boolean b = db.delete(DATABASE_TABLE, pkstr, null) > 0;
		this.closeDB();
		return b;
	}

	/**
	 * 获取所有记录
	 * 
	 * @param DATABASE_TABLE
	 * @param str
	 * @return
	 */
	public Cursor getAllRecords(String DATABASE_TABLE) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from " + DATABASE_TABLE, null);
		return c;
	}

	/**
	 * 查询满足条件的所有记录
	 * 
	 * @param DATABASE_TABLE
	 * @param str
	 * @return
	 */
	public Cursor queryRecordByCondtion(String DATABASE_TABLE, String str) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor mCursor = db.rawQuery("select * from " + DATABASE_TABLE + " "
				+ str, null);
		return mCursor;
	}

	/**
	 * 根据主键查询记录
	 * 
	 * @param DATABASE_TABLE
	 * @param idColumn
	 * @param pkid
	 * @return
	 * @throws SQLException
	 */
	public Cursor getRecordByPK(String DATABASE_TABLE, String idColumn,
			String pkid) {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor mCursor = db.query(true, DATABASE_TABLE, null, idColumn + "='"
				+ pkid + "'", null, null, null, null, null);
		int num = mCursor.getCount();
		if (num > 0) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	/**
	 * 更新记录
	 * 
	 * @param DATABASE_TABLE
	 * @param sqlstr
	 * @param idColumn
	 * @param pkstr
	 * @return
	 */
	public boolean updateRecord(String DATABASE_TABLE, ContentValues sqlstr,
			String idColumn, String pkstr) {
		SQLiteDatabase db = helper.getWritableDatabase();
		boolean b = db.update(DATABASE_TABLE, sqlstr, idColumn + "='" + pkstr
				+ "'", null) > 0;
		return b;
	}

	/**
	 * 获取游标
	 * 
	 * @param sqlStr
	 * @return
	 */
	public Cursor getCursor(String sqlStr) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor c = db.rawQuery(sqlStr, null);
		return c;
	}

	/**
	 * 关闭数据库
	 */
	public void closeDB() {
		helper.close();
	}
}
